package com.gdglc.spring.biz.impl.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gdglc.spring.biz.auth.StaffInfoBiz;
import com.y2.authprj.bean.auth.model.Page;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.bean.auth.vo.StaffInfoVo;
import com.y2.authprj.bean.auth.vo.UserroleVo;
import com.y2.authprj.dao.auth.IStaffDao;
import com.y2.authprj.exception.BizException;

public class StaffInfoBizImpl implements StaffInfoBiz{

	private IStaffDao iStaffDao;
	
	public IStaffDao getiStaffDao() {
		return iStaffDao;
	}

	public void setiStaffDao(IStaffDao iStaffDao) {
		this.iStaffDao = iStaffDao;
	}

	@Override
	public List<StaffInfo> findAll() throws BizException{
		// TODO Auto-generated method stub
		return iStaffDao.findAll();
	}
	//建飞的代码开始
		public StaffInfo checkLogin(String staffNum,String pwd,String loginIp) throws BizException{
			StaffInfo staffInfo=showByStaffNum(staffNum);
			Encryption encryption=new Encryption();
			try {
				
				boolean flag=encryption.checkpassword(pwd,staffInfo.getPwd());
				if(!flag){
					throw new BizException("密码错误!");
				}
				
				staffInfo.setLoginIp(loginIp);
				staffInfo.setLoginTime(new Date());
				updateLogin(staffInfo);	
				return staffInfo;			
			} catch (Exception e) {
				throw new BizException("密码错误!",e);
			}

		}

		@Override
		public StaffInfo showByStaffNum(String staffNum) throws BizException {
			if(null==staffNum || "".equals(staffNum)){
				throw new BizException("请确认账号!");
			}
			StaffInfo staffInfo=iStaffDao.findByStaffNum(staffNum);
			if(null==staffInfo || !staffInfo.getStaffNum().equals(staffNum)){
				throw new BizException("请确认账号");
			}
			return staffInfo;
		}

		@Override
		public Page<StaffInfo> showStaff(UserroleVo vo) throws BizException{
			Page<StaffInfo> tStaffPage=new Page<StaffInfo>();
			try{
				int count=iStaffDao.findByTStaffCount(vo);
				List<StaffInfo> tStaffList = iStaffDao.showStaff(vo);
				
				tStaffPage.setTotalCount(count);
				tStaffPage.setPageSize(vo.getNum());
				tStaffPage.setCurrPageNo(vo.getPageNum());
				int totalPage=(count%vo.getNum()==0)?(count/vo.getNum()):(count/vo.getNum())+1;
				//总页数
				tStaffPage.setTotalPageCount(totalPage);
				
				
				if(null==tStaffList){
					tStaffList = new ArrayList<StaffInfo>();
				}
				tStaffPage.setList(tStaffList);
				return tStaffPage;
			}catch(Exception e){
				throw new BizException("查询失败");
			}
		}
		
		@Override
		public void updateLogin(StaffInfo staffInfo) throws BizException {
			int i=0;
			try {
				i=iStaffDao.updateByStaff(staffInfo);
			} catch (Exception e) {
				throw new BizException("修改失败",e);
			}
			if(i==0){
				throw new BizException("您修改的记录已经不存在");
			}
		}
		
		
		//建飞的代码结束
		
		
		@Override
		public int addSta(StaffInfo sta) {
			// TODO Auto-generated method stub
			return iStaffDao.addSta(sta);
		}

		

		@Override
		public int deleteStaById(int id) {
			// TODO Auto-generated method stub
			return iStaffDao.deleteStaById(id);
		}

		@Override
		public StaffInfo StaffInfoById(int id) {
			return iStaffDao.StaffInfoById(id);
		}

		@Override
		public List<StaffInfo> findByStaff(StaffInfoVo vo) {
			return iStaffDao.findByStaff(vo);
		}

		@Override
		public int updateStaff(StaffInfo staffInfo) {
			return iStaffDao.updateSta(staffInfo);
		}

		public int findStaffNum(String staffNum){
			boolean flag=false;			
			int i=0 ;
			i =iStaffDao.findStaffNum(staffNum);
			return i;
		}
		
		public List<StaffInfoVo> findAllVo(StaffInfoVo vo){
			// TODO Auto-generated method stub
			return iStaffDao.findAllVo(vo);
		}
	
	
	
}
